import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Request,
} from "@nestjs/common";
import { Roles } from "src/utils/decorators/roles.guard";
import { TypeService } from "./type.service";
import { CreateTypeDto } from "./dto/create-type.dto";
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";

@Controller("type")
@ApiTags("商品类型")
@ApiBearerAuth()
export class TypeController {
  constructor(private readonly typeService: TypeService) {}

  // 列出所有类型列表，如果是管理员，列出所有，如果不是，列出当前用户所在公司的所有
  @Get("list")
  list(@Request() req) {
    if (req.user.aud === "admin") {
      return this.typeService.findAll();
    } else {
      const companyId = req.user.company;
      return this.typeService.findByCompany(companyId);
    }
  }

  // 为当前公司创建类型，只有经理拥有该权限
  @Post()
  create(@Request() req, @Body() createTypeDto: CreateTypeDto) {
    const companyId = req.user.company;
    return this.typeService.create(createTypeDto, companyId);
  }

  // 列出公司id对应的所有类型列表
  @Get("list/:companyId")
  @Roles("admin")
  listByCompany(@Param("companyId") companyId: string) {
    return this.typeService.findByCompany(companyId);
  }

  // 根据ID查找类型
  @Get(":id")
  find(@Param("id") id: string) {
    return this.typeService.find(id);
  }

  // 删除类型，只有经理拥有该权限
  @Delete(":id")
  @Roles("manager", "admin")
  remove(@Param("id") id: string) {
    return this.typeService.remove(id);
  }
}
